package com.cleaningcloud.cld.controller;

import com.cleaningcloud.cld.api.domain.CldStaff;
import com.cleaningcloud.cld.api.domain.Vo.CldStaffDto;
import com.cleaningcloud.cld.domain.vo.CldStaffVo;
import com.cleaningcloud.cld.service.ICldStaffService;
import com.cleaningcloud.common.core.utils.StringUtils;
import com.cleaningcloud.common.core.utils.poi.ExcelUtil;
import com.cleaningcloud.common.core.web.controller.BaseController;
import com.cleaningcloud.common.core.web.domain.AjaxResult;
import com.cleaningcloud.common.core.web.page.TableDataInfo;
import com.cleaningcloud.common.log.annotation.Log;
import com.cleaningcloud.common.log.enums.BusinessType;
import com.cleaningcloud.common.security.annotation.RequiresPermissions;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 人员花名册Controller
 *
 * @author cleaningcloud
 * @date 2024-10-12
 */
@RestController
@RequestMapping("/staff")
public class CldStaffController extends BaseController {
    @Autowired
    private ICldStaffService cldStaffService;

    /**
     * 查询人员花名册列表
     */
    @RequiresPermissions("cld:staff:list")
    @GetMapping("/list")
    public TableDataInfo list(CldStaffDto cldStaff) {
        startPage();
        List<CldStaffVo> list = cldStaffService.selectCldStaffList(cldStaff);
        return getDataTable(list);
    }

    /**
     * 离职统计
     *
     * @return
     */
    @GetMapping("/selectCldStaffAnalyze")
    public AjaxResult selectCldStaffAnalyze() {
        return success(cldStaffService.selectCldStaffAnalyze());
    }

    /**
     * 导出人员花名册列表
     */
    @RequiresPermissions("cld:staff:export")
    @Log(title = "人员花名册", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CldStaffDto cldStaff) {
        List<CldStaffVo> list = cldStaffService.selectCldStaffList(cldStaff);
        ExcelUtil<CldStaffVo> util = new ExcelUtil<CldStaffVo>(CldStaffVo.class);
        util.exportExcel(response, list, "人员花名册数据");
    }

    @Log(title = "人员花名册", businessType = BusinessType.IMPORT)
    @RequiresPermissions("cld:staff:import")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<CldStaff> util = new ExcelUtil<CldStaff>(CldStaff.class);
        List<CldStaff> staffList = util.importExcel(file.getInputStream());
        String operName = SecurityUtils.getUsername();
        String message = cldStaffService.importStaff(staffList, updateSupport, operName);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        ExcelUtil<CldStaff> util = new ExcelUtil<CldStaff>(CldStaff.class);
        util.importTemplateExcel(response, "员工数据");
    }


    /**
     * 获取人员花名册详细信息
     */
    @RequiresPermissions("cld:staff:query")
    @GetMapping(value = {"/", "/{staffId}"})
    public AjaxResult getInfo(@PathVariable("staffId") Long staffId) {
        return success(cldStaffService.selectCldStaffByStaffId(staffId));
    }

    /**
     * 新增人员花名册
     */
    @RequiresPermissions("cld:staff:add")
    @Log(title = "人员花名册", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CldStaff cldStaff) {
        if (!cldStaffService.checkUserPhoneUnique(cldStaff)) {
            return error("保存员工失败，该手机号已存在");
        }
        if (!cldStaffService.checkUserIdCardUnique(cldStaff)) {
            return error("保存员工失败，该身份证已存在");
        }
        return toAjax(cldStaffService.insertCldStaff(cldStaff));
    }


    /**
     * 新增人员花名册
     */
    @RequiresPermissions("cld:staff:reinstatement")
    @PostMapping("/reinstatementStaff/{staffId}")
    public AjaxResult reinstatementStaff(@PathVariable Long staffId) {
        return toAjax(cldStaffService.reinstatementStaff(staffId));
    }

    /**
     * 修改人员花名册
     */
    @RequiresPermissions("cld:staff:edit")
    @Log(title = "人员花名册", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CldStaff cldStaff) {
        if (!cldStaffService.checkUserPhoneUnique(cldStaff)) {
            return error("修改员工失败，该手机号已存在");
        }
        if (!cldStaffService.checkUserIdCardUnique(cldStaff)) {
            return error("修改员工失败，该身份证已存在");
        }
        return toAjax(cldStaffService.updateCldStaff(cldStaff));
    }


    /**
     * 员工离职
     */
    @RequiresPermissions("cld:staff:resign")
    @Log(title = "人员花名册", businessType = BusinessType.UPDATE)
    @PutMapping("/resign")
    public AjaxResult resign(@RequestBody CldStaff cldStaff) {
        return toAjax(cldStaffService.resignCldStaff(cldStaff));
    }

    /**
     * 删除人员花名册
     */
    @RequiresPermissions("cld:staff:remove")
    @Log(title = "人员花名册", businessType = BusinessType.DELETE)
    @DeleteMapping("/{staffIds}")
    public AjaxResult remove(@PathVariable Long[] staffIds) {
        return toAjax(cldStaffService.deleteCldStaffByStaffIds(staffIds));
    }


    /**
     * 查询人员花名册列表
     */
    @GetMapping("/getList")
    public AjaxResult getList(CldStaffDto cldStaff) {
        List<CldStaffVo> list = cldStaffService.selectCldStaffList(cldStaff);
        return success(list);
    }

    /**
     * 重置密码
     */
    @PutMapping("/resetPwd")
    public AjaxResult resetPwd(@RequestBody CldStaffDto cldStaffDto) {
        if (StringUtils.isEmpty(cldStaffDto.getStaffPassword())) {
            cldStaffDto.setStaffPassword("123456");
        }
        cldStaffDto.setStaffPassword(SecurityUtils.encryptPassword(cldStaffDto.getStaffPassword()));
        cldStaffDto.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(cldStaffService.updateCldStaff(cldStaffDto));
    }

}
